<?php
namespace App\Http\Controllers\Api\Song;
use App\Enums\Api\Response;
use App\Http\Controllers\Controller;
use App\Models\Category\Category;
use App\Models\Category\CategoryModule;
use App\Models\TextbookAudio\TextbookAudio;
use App\Models\Tool\FileUrl;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SongController extends Controller
{
    public function index(Request $request):JsonResponse
    {
        $gradeId=$request->input('gradeId');
        $pageId=$request->input('pageId')??1;
        $pageList=Category::query()->select('id','parent_id')
            ->with('textbook_audio')->has('textbook_audio')
            ->where('parent_id',$gradeId)->orderBy('id')->offset($pageId-1)
       ->first();
        if ($pageList==null || $pageList=='') {
            return apiResponse(code:Response::SYSTEM_DATA_NOT_EXISTS->value,data:[],msg: Response::SYSTEM_DATA_NOT_EXISTS->response(),type: 'virtual_page');
        }
        $file_url=FileUrl::query()->where('module',CategoryModule::ModuleTypeFive)->first();

        $data=[
            'unitID'=>$pageList->textbook_audio[0]->category_id,
            'unitName'=>$pageList->textbook_audio[0]->unit_name,
            'total_page'=>Category::query()->select('id','parent_id')->has('textbook_audio')
                ->where('parent_id',$gradeId)->orderBy('id')->count(),
            'songs'=>array_map(function($audio)use($file_url,$gradeId){
                return [
                    'ID'=>$audio['id'],
                    'title'=>$audio['title'],
                    'originalSinger'=>convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address:  $audio['original_singer']??'',grade:$gradeId,category:$audio['category_id'],type: '.mp3',file_url: $file_url),
                    'accompanimentName'=>convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address:  $audio['vocal_accompaniment']??'',grade:$gradeId,category:$audio['category_id'],type: '.mp3',file_url: $file_url),
                    'subtitlesName'=>$audio['id'],
                    'videoName'=>convert_file_address_exist(module:CategoryModule::ModuleTypeFour,file_address:  $audio['video']??'',grade:$gradeId,category:$audio['category_id'],type: '.mp4',file_url: $file_url),
                    'duration'=>$audio['duration'],
                ];
            },$pageList->textbook_audio->toArray()),
        ];
        return apiResponse(code: Response::SERVICE_SUCCESS->value, data: $data, msg: Response::SERVICE_SUCCESS->response(), type: 'song');
    }
    public function caption(int $songId):JsonResponse
    {
        $captions=TextbookAudio::query()->where('id',$songId)->value('captions');
        if ($captions==null && $captions=='') {
            return apiResponse(code:Response::SYSTEM_DATA_NOT_EXISTS->value,data:[],msg: Response::SYSTEM_DATA_NOT_EXISTS->response(),type: 'virtual_page');
        }
        $data=$this->check_captions($captions);
        return apiResponse(code: Response::SERVICE_SUCCESS->value, data: $data, msg: Response::SERVICE_SUCCESS->response(), type: 'song');
    }
    protected function check_captions(string $string):array
    {
        $array=explode("\n\n",$string);
        $new_array=[];
        if (empty($array) || $string=='' || $string==null) {
            return  [];
        }

        foreach ($array as $item) {
            $new_item=explode("\n",$item);
            if (isset($new_item[1])) {
                $time=explode("-->",$new_item[1]);
                $data['id']=$new_item[0];
                $data['startTime']=trim($time[0]);
                $data['endTime']=trim($time[1]);
                $data['text']=$new_item[2];
                $new_array[]=$data;
            }
        }
        return $new_array;
    }
}
